//
//  MineHeader.m
//  体育
//
//  Created by  on 2022/10/23.
//

#import "MineHeader.h"
#import "MineCell.h"

@interface MineHeader()
@property (strong, nonatomic) IBOutlet UIButton *avtar;
@property (strong, nonatomic) IBOutlet UIButton *username;
@property (strong, nonatomic) IBOutlet UILabel *account;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSArray *titles;
@end
@implementation MineHeader
- (void)awakeFromNib{
    [super awakeFromNib];
    self.titles = @[@"清理缓存",@"帮助反馈",@"隐私政策"];
    self.tableView.layer.shadowColor = [UIColor colorHex:@"#9B9B9B"].CGColor;
    self.tableView.layer.shadowOffset = CGSizeMake(0, 3);
    self.tableView.layer.shadowRadius = 12;
    self.tableView.layer.cornerRadius = 12;
    self.tableView.layer.shadowOpacity = 1;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[MineCell nib] forCellReuseIdentifier:[MineCell ID]];
    [self.avtar setImage:[UIImage imageWithData:APP.user.headImg] forState:UIControlStateNormal];
    [self.username setTitle:LS(APP.user.username) forState:UIControlStateNormal];
    self.account.hidden = [APP.user.account isEqualToString:defaulUserID];
    self.account.text = APP.user.account;
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.block) {
        if ([APP.user.account isEqualToString:defaulUserID] ) {
            self.block(LS(@"去登录"));
        }else if (sender.tag == 201){
            self.block(@"更换头像");
        }
    }
   
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:[MineCell ID]];
    cell.name.text = LS(self.titles[indexPath.row]);
    cell.img.image = [UIImage imageNamed:self.titles[indexPath.row]];
    cell.line.hidden = indexPath.row == [self.titles count] - 1;
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.block) {
        self.block(self.titles[indexPath.row]);
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == self.titles.count - 1) {
        UIBezierPath *path;
        if (indexPath.row == 0) {
            path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
        }else{
            path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
        }
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = cell.bounds;
        layer.path = path.CGPath;
        cell.layer.mask = layer;
    }else{
        cell.layer.mask = nil;
    }
}
@end
